package com.billiards.controller;

import com.billiards.config.PaginationResponse;
import com.billiards.entity.User;
import com.billiards.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    public UserService userService;
    @RequestMapping("/list")
    public List<User> list(){
        return userService.selectAll();
    }


    @PostMapping("/add")
    public int addUser(@RequestBody User user){

        System.out.println("===========");
        System.out.println(user);
        return userService.addUser(user);
    }
    @PostMapping("/update")
    public int updateUser( @RequestBody User user){
        return userService.updateUser(user);
    }
    @GetMapping("/delete")
    public int deleteUser(int id){
       return userService.deleteUser(id);
    }



//    分页
//    @RequestMapping("/getUserList")
//
//    public String getUserList( @RequestParam(value = "page", defaultValue = "1") int page,
//                                  @RequestParam(value = "count", defaultValue = "8") int count,
//                                  Model model) {
//        model.addAttribute("userFrom.vue",userService.getUserList(page,count));
//        return "index";
//    }
//        model.addAttribute("articlepage",articlePageInfo); articlepage是传到前端的那个文件夹

    // 注入 UserService，用于获取用户列表


    // 构造函数注入 UserService
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 使用 @GetMapping 替代 @RequestMapping(method = RequestMethod.GET)
    @GetMapping("/getUserList")
    public PaginationResponse<User> getUserList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "count", defaultValue = "8") int count) {
        // 调用 userService 的 getUserList 方法来获取分页数据
        PaginationResponse<User> response = userService.getUserList(page, count);
        // 注意：这里不应该调用 deleteUser 方法，因为它与分页逻辑无关
        // int i = deleteUser((Integer) response); // 这行应该被移除
        return response; // 直接返回 PaginationResponse 对象，Spring 会自动将其转换为JSON
    }




    //用户登录退出
    @PostMapping("/login")
    public String login(@RequestBody User user, HttpSession session){
        String result = userService.getUserByUserNameAndPassword(user,session);
        return result;
    }

    @PostMapping("/logout")
    public ResponseEntity<String> logout(HttpSession session){
//        session清空
        session.invalidate();
        return ResponseEntity.ok("Logout successful");
    }






    @GetMapping("/getUserUsername")
    public List<User> getUserUsername (String username){
        return userService.getUserUsername(username);
    }


// ------------- 9.3 ------------------------------

    //	修改个人信息
//    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
//    public R updateUser(User user, HttpServletRequest request) {
//
//        if (userService.update(user)) {
//            user = userService.findById(user.getUserId());
//            return R.ok().put("user",user);
//        } else {
//            return R.error("您要修改的用户不存在");
//        }
//    }
// --------------------------------------------------



}
